<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.kb.common.po.Kb_Cate"%>
<%@page import="com.kb.dao.CateDao"%>
<%@page import="com.kb.common.po.Kb_Term_Final"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>开始创建</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link rel="stylesheet" type="text/css" href="./css/gcss.css">
	<script charset="utf-8" src="./js/kindeditor/kindeditor-min.js"></script>
	<script src="./js/Tree.js" type="text/javascript"></script>
	<script>
			var editor;
			KindEditor.ready(function(K) {
				editor = K.create('textarea[name="content"]', {
					syncType : '',
					uploadJson : 'js/kindeditor/jsp/upload_json.jsp',
					fileManagerJson : 'js/kindeditor/jsp/file_manager_json.jsp',
					allowFileManager : true,
					afterBlur : function(){editor.sync();}
				});
				})
	</script>
	<script type="text/javascript">
		function update_index(){
			var str = document.getElementById("content").value;
			var s = document.createElement('div'); 
			s.innerHTML = str;
			var arr = s.getElementsByTagName("h1");
			var index_str = "";
			
			for(var i=0; i<arr.length; i++)
				index_str += arr[i].innerHTML.replace(/&nbsp;/g,"") + "\r\n";
				
			document.getElementById("index").value = index_str;
			//alert(index_str);
			//alert(document.getElementById("content").value);
		}
	</script>
	<script type="text/javascript">    
	function callback(msg,path)    
	{    
     	document.getElementById("file").outerHTML = document.getElementById("file").outerHTML;    
     	document.getElementById("msg").innerHTML += "<span id=\"" + msg + "span\">" + msg + " <input type=\"submit\" value=\"删除\" onclick=\"document.getElementById('deleteString').value='" + msg + "'\"/></span><br><br>";
     	//document.getElementById("pathstring").innerHTML += path + ":";
	}    
	</script>
	<script type="text/javascript">    
	function deleteAttached(msg, filename)
	{
     	document.getElementById(filename + "span").parentNode.removeChild(document.getElementById(filename + "span"));
     	document.getElementById("pathstring").innerHTML = document.getElementById("pathstring").innerHTML.replace("attached/termattached/" + msg + ":", "");
	}    
	</script>
	<script type="text/javascript" >
	 function submit(){
	        //判断词条不能为空
	        var thisform=document.getElementById('form');
	        with (thisform){
  				if (thisform.content.value.replace(/[ ]/g,"")==""){
  					alert("词条内容不能为空。");
  					return false;
  				}
  			}
  			//判断词条名片不能为空
	        with (thisform){
  				if (thisform.message.value.replace(/[ ]/g,"")==""){
  					alert("词条名片不能为空。");
  					return false;
  				}
  			}
  			//判断是否是正确URL
  			if(!ifURL())
  			{
  			return false;
  			}
  			//全选list2，使得servlet可以获取list2所有参数
  			var selectedComs = document.getElementById("list2");
            for(var i=0;i<selectedComs.length;i++){
                selectedComs.options[i].selected = true;
            }
            //判断list2不能为空
  	         var len = document.getElementById('list2').options.length;
             if(!len>0)
             {
             alert("请选择分类名称");
                return false;
             }
             
            //获取上传文件的相对路径
            document.getElementById('attachedpath').value=document.getElementById('pathstring').innerHTML;
            //对词条内容进行转码，解决乱码问题
            document.getElementById('content').value=HTMLEncode(document.getElementById('content').value);
            //alert(document.getElementById('content').value);
            //return false;
            //提交
            document.getElementById('form').submit();
	 }
	</script>
	<script LANGUAGE="JavaScript">  
   <!--//  
   function textCounter(field, countfield, maxlimit) {  
   //alert("ok");
   // 函数，3个参数，表单名字，表单域元素名，限制字符；  
   if (field.value.length > maxlimit)  
   //如果元素区字符数大于最大字符数，按照最大字符数截断；  
   field.value = field.value.substring(0, maxlimit);  
   else  
   //在记数区文本框内显示剩余的字符数；  
   countfield.value = maxlimit - field.value.length;  
   }  
   //-->  
   </script>  
   
    <script language="javascript">
	function ifURL()
	{
		var urlreg=/^((https|http|ftp|rtsp|mms)?:\/\/)+[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/
		var url=document.form.urllink.value
		var urls=url.split(",");
		var flag=1;
		for(i=0;i<urls.length;i++){
			if (!urlreg.test(urls[i]))
			{
				alert("网址错误！")
				return false
    		}else{
    		flag=1;
			}
		}
		if(flag==1){
		return true;
		}
		
	}
   </script>
   
   
  </head>
  
  <body>
  <%
  //String[][] catename=(String[][])request.getAttribute("catename");
  CateDao catedao=new CateDao();
  List<Kb_Cate> cate=catedao.getTotalCate();
  String[][] name=new String[cate.size()][2];//用于存放各级目录名称和对应的cate_index
	for(int i=0;i<cate.size();i++){
		Kb_Cate kb_cate=new Kb_Cate();
		kb_cate=cate.get(i);
		String index=kb_cate.getCate_Index();
		String catename=kb_cate.getCate_Name();
		String[] temp=index.split("-");
			
		for(int j=0;j<temp.length;j++){
			if(j==0){
				name[i][0]="&nbsp;&nbsp;&nbsp;";
				name[i][1]=index;
			}
				name[i][0]+="&nbsp;&nbsp;&nbsp;";
				name[i][1]=index;
			}
			name[i][0]+=catename;
		}
		
	Kb_Term_Final termfinal=(Kb_Term_Final)request.getAttribute("term_final");
	String[] temp=termfinal.getFinal_Index().split(",");
	String final_index="";
	for(int i=0;i<temp.length;i++){
	    if(temp[i]!=null&&temp[i].length()>0){
	       final_index+=temp[i]+"\r\n";
	    }      
	}
	String[][] catename=catedao.getCateNameByCateIndex(termfinal.getFinal_Cate());
	
	String content=termfinal.getFinal_Content();
	out.println("<script>parent.HTMLEncod('"+content+"')</script>"); 
	
  /*if(catename!=null&&catename.length>0)
  {	
     out.println("<script>alert('查询分类成功！')</script>");
  } 
  else
  {
     out.println("<script>alert('查询分类失败！')</script>");
  }*/
   %>
		<table width="100%">
			<tr>
				<td height="20" align="left">
					<a href="./index.jsp">首页</a>
				</td>
				<td height="20" align="right">
					<%
						String logLable = null;
						if (request.getSession().getAttribute("loginName") == null)
							logLable = "<a href=\"account/login.jsp\">登录</a> <a href=\"account/register.jsp\">注册</a>";
						else
							logLable = request.getSession().getAttribute("loginName")
									.toString()
									+ " " + "<a href=\"index.jsp?loginout=true\">退出登录</a>";
					%>
					<%=logLable%>
				</td>
			</tr>
		</table>
		<hr>
		<form action="./term/TermServlet?action=a" id="form" name="form" method="post" onSubmit="return validate_form(this);" enctype="application/x-www-form-urlencoded">
		<table class="ettb" width="100%" border="0">
			<tr>
				<td width="80">词条名称：</td>
				<td>
					<input class="text" id="title" name="title" type="text" size="50" value="<%=request.getAttribute("termname")%>"  readonly="readonly">
				</td>
			</tr>
			<tr>
				<td valign="top">词条内容：</td>
				<td>
					<table>
					<tr><td>
					<textarea id="content" name="content" cols="100" rows="25"><%=content%></textarea>
					</td>
					<td>
					<input type="button" value=" >>&#13;&#10;生成目录&#13;&#10;>>" onClick="update_index();" style="height=380px;width=80px;">
					<textarea id="index" name="index" cols="25" rows="25"  style="overflow-y:hidden " readonly="readonly" ><%=final_index%></textarea>
					</td></tr>
					</table>
				</td>
			</tr>
			<tr>
				<th height="61">词条名片</th>
				<th align="left"><p>
				 <textarea name="message" cols="110" rows="3" onKeyDown="textCounter(message,remLen,140);" onKeyUp="textCounter(message,remLen,140);"><%=termfinal.getFinal_Card()%></textarea>  
        您还可以输入:<input name="remLen" type="text" value="140" size="5" readonly="readonly">个字符  
				
			    </p>
			    </th>		
			</tr>
			<tr>
				<th height="61">参考资料</th>
				<th align="left"><p><textarea name="urllink" cols="140" rows="3"><%=termfinal.getFinal_Url()%></textarea>
				<font color="red">参考资料URL链接，用,分隔</font>
				<input type="hidden" value="" name="path" id="attachedpath"/>
				<input name="creator" type="hidden" value="<%=request.getSession().getAttribute("loginID")%>" />
				<!-- statu=0保存，statu=1提交审核 --> 
				<input type="hidden" value="" name="statu" id="statu"/>
			    </p>
			    </th>		
			</tr>
			
		<p><tr>
		<th>分类名称</th>
					<td>
						<select multiple size="7" name="list1" style="width: 250px">
						<%
						for(int i =0;i<name.length;i++)
						{
						%>
						    <option value=<%=name[i][1]%>>
						        <%=name[i][0] %>
							</option>
						<%} %>
						</select>
					
						<input type="button" value="    >>   "
							onclick="moveall('list1','list2')" name="B3">
						<input type="button" value="   >   "
							onclick="move('list1','list2')" name="B1">
						<input type="button" value="   <   "
							onclick="del('list2')" name="B2">
						<input type="button" value="    <<   "
							onclick="delall('list2')" name="B4">
					
						<select multiple size="7" name="list2" style="width: 250px">
							<%
							//String[] catenamesplit=catename.split(",");//去除所有分类的cate_index
							for(int i=0;i<catename.length;i++){			
							%>
							<option value=<%=catename[i][1]%>>
						        <%=catename[i][0] %>
							</option>
							<%} %>
						</select>
					</td>
				</tr></p>	
		  </tr>	
		</table>
		</form>
		 <form action="./term/upload.jsp" id="form1" name="form1" encType="multipart/form-data" method="post" target="hidden_frame" >    
      <input type="file" id="file" name="file" style="width:450">    
      <INPUT type="submit" value="上传文件">
      <br>
      <font color="red">支持doc,txt,jpg,rar,mid,waw,mp3,gif,pdf文件的上传</font>
      <p>
      <input type="hidden" name="wordname" value="<%=request.getAttribute("termname") %>"/>
      <iframe name='hidden_frame' id="hidden_frame" style="display:none"></iframe>    
	</form>
	<form action="./term/delete.jsp" id="form2" name="form2" method="post" target="delete_frame" >
		<span id="pathstring">已上传的参考资料：</span>
		<p></p>
		<span id="msg"></span>
    	<input type="hidden" name="deleteString" id="deleteString"/>
    	<input type="hidden" name="wordname" value="<%=request.getAttribute("termname") %>"/>
    	<iframe name='delete_frame' id="delete_frame" style='display:none'></iframe>
    	<%
    	String wordpath_temp=termfinal.getFinal_Attachment_Id();
    	String pathname_temp=termfinal.getFinal_Attachment_Name();
    	String[] wordpath_xd=wordpath_temp.split(":");//相对路径
    	String[] pathname=pathname_temp.split(":");//文件名+扩展名
    	for(int i=0;i<wordpath_xd.length;i++){
    	out.println("<script>parent.callback('"+pathname[i]+"','" + wordpath_xd[i] + "')</script>"); 
    	}
    	
 		
    	
    	 %>
    </form>

    <p><th align="left">	
			<input class="button" type="button" name="check" value="提交审核" onClick="document.getElementById('statu').value=1;submit()"/>
			<input class="button" type="button" name="save" value="保存到草稿箱" onClick="document.getElementById('statu').value=0;submit()"/>
			<input class="button" type="button" onClick="window.history.back();" value="返 回" />
	</th></p>
	
	</body>
</html>
